package com.lightdz.tshirtdz.bll;

import com.lightdz.tshirtdz.dal.impl.ReceiveAddrDal;
import com.lightdz.tshirtdz.model.api.ApiReceiveAddr;
import com.lightdz.tshirtdz.model.api.ApiResponse;
import com.lightdz.tshirtdz.model.entity.ReceiveAddrEntity;

import com.lightdz.tshirtdz.utils.LogHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ Description：${description}
 * @ Author     ：zbl
 * @ Date       ：Created in 16:54 09/08/2018
 * @ Modified By：
 */
@Repository
public class ReceiveAddrBll {

    @Autowired
    ReceiveAddrDal addrDal;

    /**
     * 添加收货地址
     * @param isDefault
     * @param receiver
     * @param receiverTel
     * @param regionAddr
     * @param wxId
     * @param streetAddr
     * @return
     */
    public ApiResponse<Boolean> addAddr(boolean isDefault, String receiver, String receiverTel, String regionAddr, String wxId, String streetAddr) {
        ApiResponse<Boolean> apiResponse = ApiResponse.getNoResponse();

        ReceiveAddrEntity entity = null;
        try {
            entity = new ReceiveAddrEntity();
            entity.setCreatetime(new Timestamp(new Date().getTime()));
            entity.setIsdefault(isDefault ? 0 : 1);
            entity.setReceiver(receiver);
            entity.setReceivetel(receiverTel);
            entity.setRegionAddr(regionAddr);
            entity.setUpdatetime(new Timestamp(new Date().getTime()));
            entity.setWxid(wxId);
            entity.setStreetAddr(streetAddr);
            entity.setReceiveaddr(regionAddr + streetAddr);
            boolean result =  addrDal.addAddr(entity);
            apiResponse=new ApiResponse<>(result);
        } catch (Exception e) {
            e.printStackTrace();
            LogHelper.error("ReceiveAddrBll.addAddr", e);
        }
        return apiResponse;
    }

    /**
     * 修改收货地址
     * @param addrId
     * @param isDefault
     * @param receiver
     * @param receiverTel
     * @param regionAddr
     * @param wxId
     * @param streetAddr
     * @return
     */
    public ApiResponse<Boolean>  updateAddr(long addrId, boolean isDefault, String receiver, String receiverTel, String regionAddr,String wxId, String streetAddr) {
        ApiResponse<Boolean> apiResponse = ApiResponse.getNoResponse();

        try {
            boolean result=false;
            ReceiveAddrEntity entity = addrDal.getOneAddr(wxId, addrId);
            if (entity != null) {
                entity.setIsdefault(isDefault ? 0 : 1);
                entity.setReceiver(receiver);
                entity.setReceivetel(receiverTel);
                entity.setRegionAddr(regionAddr);
                entity.setUpdatetime(new Timestamp(new Date().getTime()));
                entity.setReceiveaddr(regionAddr + streetAddr);
                entity.setStreetAddr(streetAddr);

                result = addrDal.updateAddr(entity);
            }
            apiResponse=new ApiResponse<>(result);
        } catch (Exception e) {
            e.printStackTrace();
            LogHelper.error("ReceiveAddrBll.updateAddr", e);
        }
        return  apiResponse;
    }

    /**
     * 获取我的收货地址
     * @param wxId
     * @return
     */
    public ApiResponse<List<ApiReceiveAddr>> getMyAddrList(String wxId) {
        ApiResponse<List<ApiReceiveAddr>> apiResponse = ApiResponse.getNoResponse();

        List<ApiReceiveAddr> addrList = new ArrayList<>();

        try {
            List<ReceiveAddrEntity> addrEntityList = addrDal.getMyAddr(wxId);
            if (!addrEntityList.isEmpty()) {

                for (ReceiveAddrEntity addr : addrEntityList) {

                    ApiReceiveAddr apiAddr = new ApiReceiveAddr();
                    apiAddr.setAddrId(addr.getTid());
                    apiAddr.setDefault(addr.getIsdefault() == 1);
                    apiAddr.setReceiver(addr.getReceiver());
                    apiAddr.setRegionAddr(addr.getRegionAddr());
                    apiAddr.setStreetAddr(addr.getStreetAddr());
                    apiAddr.setTel(addr.getReceivetel());

                    addrList.add(apiAddr);
                }
            }
           apiResponse=new ApiResponse<>(addrList);
        } catch (Exception e) {
            e.printStackTrace();
            LogHelper.error("ReceiveAddrBll.getMyAddrList", e);
        }
        return  apiResponse;
    }

    /**
     * 根据收货地址id删除收货地址
     * @param addrId
     * @return
     */
    public ApiResponse<Boolean> deleteAddr(int addrId){
        boolean result = addrDal.deleteAddr(addrId);
        return new ApiResponse<>(result);
    }
}
